// pages/integral_growth/integral_growth.js
var that,
  api = new(require('../../utils/api.js')),
  staticimg=api.getstaticimg(),
  headimg = api.getimgurl(),
  choseuid=17478,
  // md5 = require('../../utils/md5.js'),
  app = getApp(),
  util = new(require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesize:20,
    start:0,
    // all:'',
    date:'',
    fromwhere:'',
    score:'',
    recycleindex:0,
    recyclerid:0,
    recyclers:[],
    recyclersname:[],
    startdate:"",
    enddate:'',
    list:[],
    // judge:'',
    staticimg,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this
    // let user=wx.getStorageSync('user')
    // that.setData({storage:(user.storage/100).toFixed(2)})
 
    that.getdata()
  }, 



  formatlist(zonglist,recyclers,fenlist){
    let {recyclersname} = that.data;
            console.log(recyclersname,'recyclersname');
            if(recyclersname.length==0){
              recyclers.forEach(x=>{recyclersname.push(x.realname)})
    that.setData({recyclers,recyclersname})
            }
            let list = [];
            zonglist.forEach(x=>{

                let newlist = [];
                fenlist.forEach(y=>{
                  if(y.type==x.type){
                    newlist.push(y)
                  }
                })
                let ava = ((x.p/100.0)/(x.w/1000.0)).toFixed(1)
                list.push({
                  list:newlist,
                  totalprice:x.p,
                  totalweight:x.w,
                  word:`${x.typename}总和：重量：${x.w/1000}kg 均价${ava}元每kg 总价${(x.p/100).toFixed(2)}元`,
                  typename:x.typename,
                  type:x.type
                })
            })
            that.setData({list})

    // let typearr = [],newlist=[];

    // list.forEach((x,i)=>{
    //     if(typearr.indexOf(x.typename)==-1){
    //         typearr.push(x.typename)
    //     }
    // })
    // typearr.forEach(x=>{
    //     let typelist = [],priceall=0,weightall=0;
    //     list.forEach(y=>{
    //         if(y.typename==x){
    //           if(typelist.length>0){
    //             typelist.forEach((z,i)=>{
    //               if(z.recyclerid==y.recyclerid&&z.subtype==y.subtype){
    //                 typelist[i]['price']+=y.price
    //                 typelist[i]['weight']+=y.weight
    //               }else{
    //                 typelist.push(y)
    //               }
    //             })
    //           }else{
    //             typelist.push(y)
    //           }
    //             priceall+=y.price;
    //             weightall+=y.weight
    //         }
    //     })
    //         let ava = ((priceall/100.0)/(weightall/1000.0)).toFixed(1)
    //     newlist.push({
    //         list:typelist,
    //         name:x,
    //         word:`${x}总和：重量：${weightall/1000}kg 均价${ava}元每kg 总价${(priceall/100).toFixed(2)}元`,
    //         priceall,
    //         weightall,
    //     })
    // })
    // console.log(newlist,'newlistnewlist');
    // that.setData({list:newlist})

  },
  getdata(){
    wx.showLoading({
      title: '加载中',
    })
    let {list}= that.data,storage=0;
    // if(judge==2){

      api.getstatistics({choseuid},res => {
        wx.hideLoading({ })

        if(res.data.issuccess==1){
        
        that.formatlist(res.data.zonglist,res.data.recyclelist,res.data.fenlist)

        }else{
          util.nocancelmodal(res.data.msg)
        }

//       if (res.data.issuccess == 1) {
//         res.data.list.forEach(x=>{
//           let total=(x.total/100).toFixed(2)
//           storage+=Number(total)
//           x['total']=total
//           // if(x.name)
//           // x.updateDate = util.formatTimenosecend(new Date(x.updateDate))
//           // x['sourcechinese']=x.source=="CONSUMER"?'普通用户':x.source=="AGENT"?"代理商":"超管"
//           // list.push(x)
//         })
//         let {recyclersname} = that.data;
//         console.log(recyclersname,'recyclersname');
//         if(recyclersname.length==0){
//           res.data.recyclelist.forEach(x=>{recyclersname.push(x.realname)})
// that.setData({recyclers:res.data.recyclelist,recyclersname})
//         }
//         that.setData({list:res.data.list, reachbtm: list.length == res.data.total,storage})
//       }
    });
  },
  changerecycler(e){
   
    let{value} = e.detail,{recyclers}= this.data;
    that.setData({recycleindex:Number(value),recyclerid : recyclers[Number(value)].id })
    
  },
  changestart(e){
    let {value} = e.detail,d = Date.parse(value),nowts =  new Date().getTime();
    console.log(nowts-d,'ddd');
    if(nowts-d<0){
     util.nocancelmodal('起始时间不能超过当前时间')
     return;
    }
    that.setData({startdate:value})
  },

  changeend(e){

    let {value} = e.detail,d = Date.parse(value),{startdate} = this.data  ;
    
    if(startdate.length==0){
      util.nocancelmodal('请先选择起始时间')
      return;
     }
     let sd = Date.parse(startdate)

    if(d-sd<0){
     util.nocancelmodal('起始时间不能超过结束时间')
     return;
    }
    that.setData({enddate:e.detail.value})
    console.log(e,'eeechangeend');
  },

  confirm(){
    wx.showLoading({
      title: '加载中',
    })
    let{recyclerid,startdate,enddate} = this.data;
    if(recyclerid==0&&startdate==""&&enddate=="") return;
    if(startdate!=""&&enddate==""){
      util.nocancelmodal('请选择结束时间')
     return;
    }
    if(startdate==""&&enddate!=""){
      util.nocancelmodal('请选择起始时间')
     return;
    }
    let sts =startdate==''?'':Date.parse(startdate)/1000 - 60*60*8,ets = startdate==''?'':Date.parse(enddate)/1000+ 60*60*16-1;
    api.getstatistics({choseuid,sts,ets,recyclerid},res=>{
      wx.hideLoading({ })
      if(res.data.issuccess==1){
        that.formatlist(res.data.zonglist,res.data.recyclelist,res.data.fenlist)
      }
    })

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // let { reachbtm } = this.data;
    // if (reachbtm) {
    //   return
    // }
    // that.setData({ start: that.data.start + that.data.pagesize }, () => {
    //   that.getdata()
    // })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})